package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.*;
import com.itheima.domain.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

/**
 * 套餐管理
 *
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/9/27 14:12
 */

/**
 * 体检套餐管理
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;


    /**
     * 图片上传;
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        try {
            //获取原始文件名称;
            String originalFilename = imgFile.getOriginalFilename();
            //获取最后一个点的索引位置;
            int index = originalFilename.lastIndexOf(".");
            //以最后一个点的前一位进行切割,获取的就是文件的后缀名;
            String name = originalFilename.substring(index - 1);
            //使用uuid进行文件名称的拼接;
            String fileName = UUID.randomUUID().toString() + name;
            //调用七牛云工具类进行上传;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (Exception e) {
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


    /**
     * 添加数据
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 分页条件查询
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryBean queryBean) {
        PageResult pageResult = setmealService.findPage(queryBean);
        return pageResult;
    }
   /* @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }*/
}
